package com.springboot.jdk.concurrency;

import java.util.Random;
import java.util.concurrent.Semaphore;

/**
 * 分布式计算
 *
 * @author: zr
 * @date: 2021-10-09
 */
public class SpeculationComputeDemo {

    public static void main(String[] args) throws InterruptedException {
        Semaphore semaphore = new Semaphore(0);

        for (int i = 0; i < 3; i++) {
            int finalI = i;
            new Thread() {
                @Override
                public void run() {
                    try {
                        Thread.sleep((new Random().nextInt(5) + 1) * 1000);
                        System.out.println(Thread.currentThread() + "分配同一个计算任务给不同的机器。。。。。");
                        semaphore.release();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        }

        semaphore.acquire(1);
        System.out.println("一台机器执行成功了，就可以收拢计算结果了");
    }
}
